﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CS_NumberPlateReader
{
    public partial class GuraMaker : Form
    {
        public GuraMaker()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 이 코드는 데이터를 'cS_NPRDataSet.DataTemp' 테이블에 로드합니다. 필요한 경우 이 코드를 이동하거나 제거할 수 있습니다.
            this.dataTempTableAdapter.Fill(this.cS_NPRDataSet.DataTemp);
        }

        private void txt_Search_KeyDown(object sender, KeyEventArgs e)
        {
        }

        private void btn_Playback_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileName = System.IO.Path.GetFileNameWithoutExtension(dlg.FileName);
                SrcImage = Image.FromFile(dlg.FileName);
                gura = GRADataManager.GetPlateInfo_Image(System.IO.Path.GetFileNameWithoutExtension(FileName));
                
                if (gura == null)
                {
                    txtTime.Text = "00:00:00";
                    txtLicenseNumber.Text = FileName.Split('_')[1];
                    gura = new GRAData();
                    gura.Time = txtTime.Text;
                    gura.PlateNumber = txtLicenseNumber.Text;

                    DataTable dt = cS_NPRDataSet.Tables[1];

                    foreach (DataRow dr in dt.Rows)
                    {
                        dr["X1"] = 0;
                        dr["Y1"] = 0;
                        dr["X2"] = 0;
                        dr["Y2"] = 0;
                        dr["UseYN"] = false;
                    }

                    UpdateRect();
                    btnSave.Enabled = true;
                }
                else
                {
                    txtTime.Text = gura.Time;
                    txtLicenseNumber.Text = gura.PlateNumber;

                    DataTable dt = cS_NPRDataSet.Tables[1];

                    foreach(DataRow dr in dt.Rows)
                    {
                        dr["X1"] = 0;
                        dr["Y1"] = 0;
                        dr["X2"] = 0;
                        dr["Y2"] = 0;
                        dr["UseYN"] = false;
                    }

                    for (int i = 0; i < gura.RectAreaList.Count; i++)
                    {
                        Rectangle rect = gura.RectAreaList[i];
                        dt.Rows[i]["X1"] = rect.Left;
                        dt.Rows[i]["Y1"] = rect.Top;
                        dt.Rows[i]["X2"] = rect.Right;
                        dt.Rows[i]["Y2"] = rect.Bottom;
                        dt.Rows[i]["UseYN"] = true;
                    }

                    this.dataTempTableAdapter.Update(cS_NPRDataSet);
                    UpdateRect();

                    btnSave.Enabled = false;
                }
            }
        }

        private void UpdateRect()
        {
            // 가라데이터 수정
            gura.RectAreaList.Clear();
            foreach (DataRow dr in cS_NPRDataSet.Tables[1].Rows)
            {
                if ((bool)dr["UseYN"] == true)
                {
                    Rectangle rect = new Rectangle((int)dr["X1"], (int)dr["Y1"], (int)dr["X2"] - (int)dr["X1"], (int)dr["Y2"] - (int)dr["Y1"]);
                    gura.RectAreaList.Add(rect);
                }
            }



            Pen penRect = new Pen(new SolidBrush(Color.Red));

            // main
            Bitmap bm_main = new Bitmap(picMain.Width, picMain.Height);
            Graphics g_main = Graphics.FromImage(bm_main);

            // 가라데이터 생성용
            g_main.DrawImage(SrcImage, new Point(0, 0));


            foreach (Rectangle rect in gura.RectAreaList)
            {
                g_main.DrawRectangle(penRect, rect);
            }
            
            
            picMain.Image = bm_main;
        }

        private void picMain_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = picMain.PointToClient(new Point(Control.MousePosition.X, Control.MousePosition.Y));
            label3.Text = "좌표: " + p.X.ToString() + ", " + p.Y.ToString();
        }

        private string FileName = "";
        private GRAData gura = null;
        private Image SrcImage = null;

        private void btnSave_Click(object sender, EventArgs e)
        {
            GRADataManager.SetPlateInfo_Image(FileName, gura);
            btnSave.Enabled = false;
        }

        private void dataTempBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.dataTempBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.cS_NPRDataSet);

        }

        private void dataTempDataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (cS_NPRDataSet.HasChanges())
            {
                this.dataTempTableAdapter.Update(cS_NPRDataSet);
                UpdateRect();
            }
        }

   }
}
